<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>stringformat_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_string_stringformat_test.js.html">stringformat_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.string.formatTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.string.formatTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.string.format&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line20"></a>
<a name="line21"></a>// The discussion on naming this functionality is going on.
<a name="line22"></a>var f = goog.string.format;
<a name="line23"></a>
<a name="line24"></a>function testImmediateFormatSpecifier() {
<a name="line25"></a>  assertEquals(&#39;Empty String&#39;, &#39;&#39;, f(&#39;&#39;));
<a name="line26"></a>  assertEquals(&#39;Immediate Value&#39;, &#39;Immediate Value&#39;, f(&#39;Immediate Value&#39;));
<a name="line27"></a>}
<a name="line28"></a>
<a name="line29"></a>function testPercentSign() {
<a name="line30"></a>  assertEquals(&#39;%&#39;, &#39;%&#39;, f(&#39;%&#39;));
<a name="line31"></a>  assertEquals(&#39;%%&#39;, &#39;%&#39;, f(&#39;%%&#39;));
<a name="line32"></a>  assertEquals(&#39;%%%&#39;, &#39;%%&#39;, f(&#39;%%%&#39;));
<a name="line33"></a>  assertEquals(&#39;%%%%&#39;, &#39;%%&#39;, f(&#39;%%%%&#39;));
<a name="line34"></a>
<a name="line35"></a>  assertEquals(&#39;width of the percent sign ???&#39;, &#39;%%&#39;, f(&#39;%345%%-67.987%&#39;));
<a name="line36"></a>}
<a name="line37"></a>
<a name="line38"></a>function testStringConversionSpecifier() {
<a name="line39"></a>  assertEquals(&#39;%s&#39;, &#39;abc&#39;, f(&#39;%s&#39;, &#39;abc&#39;));
<a name="line40"></a>  assertEquals(&#39;%2s&#39;, &#39;abc&#39;, f(&#39;%2s&#39;, &#39;abc&#39;));
<a name="line41"></a>  assertEquals(&#39;%6s&#39;, &#39;   abc&#39;, f(&#39;%6s&#39;, &#39;abc&#39;));
<a name="line42"></a>  assertEquals(&#39;%-6s&#39;, &#39;abc   &#39;, f(&#39;%-6s&#39;, &#39;abc&#39;));
<a name="line43"></a>}
<a name="line44"></a>
<a name="line45"></a>function testFloatConversionSpecifier() {
<a name="line46"></a>  assertEquals(&#39;%f&#39;, &#39;123&#39;, f(&#39;%f&#39;, 123));
<a name="line47"></a>  assertEquals(&#39;%f&#39;, &#39;0.1&#39;, f(&#39;%f&#39;, 0.1));
<a name="line48"></a>  assertEquals(&#39;%f&#39;, &#39;123.456&#39;, f(&#39;%f&#39;, 123.456));
<a name="line49"></a>
<a name="line50"></a>  // Precisions, paddings and other flags are handled on a flag to flag basis.
<a name="line51"></a>
<a name="line52"></a>}
<a name="line53"></a>
<a name="line54"></a>function testAliasedConversionSpecifiers() {
<a name="line55"></a>  assertEquals(&#39;%i vs. %d&#39;, f(&#39;%i&#39;, 123), f(&#39;%d&#39;, 123));
<a name="line56"></a>  assertEquals(&#39;%u vs. %d&#39;, f(&#39;%u&#39;, 123), f(&#39;%d&#39;, 123));
<a name="line57"></a>}
<a name="line58"></a>
<a name="line59"></a>function testIntegerConversion() {
<a name="line60"></a>  assertEquals(&#39;%d&#39;, &#39;0&#39;, f(&#39;%d&#39;, 0));
<a name="line61"></a>
<a name="line62"></a>  assertEquals(&#39;%d&#39;, &#39;123&#39;, f(&#39;%d&#39;, 123));
<a name="line63"></a>  assertEquals(&#39;%d&#39;, &#39;0&#39;, f(&#39;%d&#39;, 0.1));
<a name="line64"></a>  assertEquals(&#39;%d&#39;, &#39;0&#39;, f(&#39;%d&#39;, 0.9));
<a name="line65"></a>  assertEquals(&#39;%d&#39;, &#39;123&#39;, f(&#39;%d&#39;, 123.456));
<a name="line66"></a>
<a name="line67"></a>  assertEquals(&#39;%d&#39;, &#39;-1&#39;, f(&#39;%d&#39;, -1));
<a name="line68"></a>  assertEquals(&#39;%d&#39;, &#39;0&#39;, f(&#39;%d&#39;, -0.1));
<a name="line69"></a>  assertEquals(&#39;%d&#39;, &#39;0&#39;, f(&#39;%d&#39;, -0.9));
<a name="line70"></a>  assertEquals(&#39;%d&#39;, &#39;-123&#39;, f(&#39;%d&#39;, -123.456));
<a name="line71"></a>
<a name="line72"></a>  // Precisions, paddings and other flags are handled on a flag to flag basis.
<a name="line73"></a>
<a name="line74"></a>}
<a name="line75"></a>
<a name="line76"></a>function testSpaceFlag() {
<a name="line77"></a>  assertEquals(&#39;zero %+d &#39;, &#39; 0&#39;, f(&#39;% d&#39;, 0));
<a name="line78"></a>
<a name="line79"></a>  assertEquals(&#39;positive % d &#39;, &#39; 123&#39;, f(&#39;% d&#39;, 123));
<a name="line80"></a>  assertEquals(&#39;negative % d &#39;, &#39;-123&#39;, f(&#39;% d&#39;, -123));
<a name="line81"></a>
<a name="line82"></a>  assertEquals(&#39;positive % 3d&#39;, &#39; 123&#39;, f(&#39;% 3d&#39;, 123));
<a name="line83"></a>  assertEquals(&#39;negative % 3d&#39;, &#39;-123&#39;, f(&#39;% 3d&#39;, -123));
<a name="line84"></a>
<a name="line85"></a>  assertEquals(&#39;positive % 4d&#39;, &#39; 123&#39;, f(&#39;% 4d&#39;, 123));
<a name="line86"></a>  assertEquals(&#39;negative % 4d&#39;, &#39;-123&#39;, f(&#39;% 4d&#39;, -123));
<a name="line87"></a>
<a name="line88"></a>  assertEquals(&#39;positive % 6d&#39;, &#39;   123&#39;, f(&#39;% 6d&#39;, 123));
<a name="line89"></a>  assertEquals(&#39;negative % 6d&#39;, &#39;-  123&#39;, f(&#39;% 6d&#39;, -123));
<a name="line90"></a>
<a name="line91"></a>  assertEquals(&#39;positive % f &#39;, &#39; 123.456&#39;, f(&#39;% f&#39;, 123.456));
<a name="line92"></a>  assertEquals(&#39;negative % f &#39;, &#39;-123.456&#39;, f(&#39;% f&#39;, -123.456));
<a name="line93"></a>
<a name="line94"></a>  assertEquals(&#39;positive % .2f &#39;, &#39; 123.46&#39;, f(&#39;% .2f&#39;, 123.456));
<a name="line95"></a>  assertEquals(&#39;negative % .2f &#39;, &#39;-123.46&#39;, f(&#39;% .2f&#39;, -123.456));
<a name="line96"></a>
<a name="line97"></a>  assertEquals(&#39;positive % 6.2f&#39;, &#39; 123.46&#39;, f(&#39;% 6.2f&#39;, 123.456));
<a name="line98"></a>  assertEquals(&#39;negative % 6.2f&#39;, &#39;-123.46&#39;, f(&#39;% 6.2f&#39;, -123.456));
<a name="line99"></a>
<a name="line100"></a>  assertEquals(&#39;positive % 7.2f&#39;, &#39; 123.46&#39;, f(&#39;% 7.2f&#39;, 123.456));
<a name="line101"></a>  assertEquals(&#39;negative % 7.2f&#39;, &#39;-123.46&#39;, f(&#39;% 7.2f&#39;, -123.456));
<a name="line102"></a>
<a name="line103"></a>  assertEquals(&#39;positive % 10.2f&#39;, &#39;    123.46&#39;, f(&#39;% 10.2f&#39;, 123.456));
<a name="line104"></a>  assertEquals(&#39;negative % 10.2f&#39;, &#39;-   123.46&#39;, f(&#39;% 10.2f&#39;, -123.456));
<a name="line105"></a>
<a name="line106"></a>  assertEquals(&#39;string % s &#39;, &#39;abc&#39;, f(&#39;% s&#39;, &#39;abc&#39;));
<a name="line107"></a>  assertEquals(&#39;string % 3s&#39;, &#39;abc&#39;, f(&#39;% 3s&#39;, &#39;abc&#39;));
<a name="line108"></a>  assertEquals(&#39;string % 4s&#39;, &#39; abc&#39;, f(&#39;% 4s&#39;, &#39;abc&#39;));
<a name="line109"></a>  assertEquals(&#39;string % 6s&#39;, &#39;   abc&#39;, f(&#39;% 6s&#39;, &#39;abc&#39;));
<a name="line110"></a>}
<a name="line111"></a>
<a name="line112"></a>function testPlusFlag() {
<a name="line113"></a>  assertEquals(&#39;zero %+d &#39;, &#39;+0&#39;, f(&#39;%+d&#39;, 0));
<a name="line114"></a>
<a name="line115"></a>  assertEquals(&#39;positive %+d &#39;, &#39;+123&#39;, f(&#39;%+d&#39;, 123));
<a name="line116"></a>  assertEquals(&#39;negative %+d &#39;, &#39;-123&#39;, f(&#39;%+d&#39;, -123));
<a name="line117"></a>
<a name="line118"></a>  assertEquals(&#39;positive %+3d&#39;, &#39;+123&#39;, f(&#39;%+3d&#39;, 123));
<a name="line119"></a>  assertEquals(&#39;negative %+3d&#39;, &#39;-123&#39;, f(&#39;%+3d&#39;, -123));
<a name="line120"></a>
<a name="line121"></a>  assertEquals(&#39;positive %+4d&#39;, &#39;+123&#39;, f(&#39;%+4d&#39;, 123));
<a name="line122"></a>  assertEquals(&#39;negative %+4d&#39;, &#39;-123&#39;, f(&#39;%+4d&#39;, -123));
<a name="line123"></a>
<a name="line124"></a>  assertEquals(&#39;positive %+6d&#39;, &#39;+  123&#39;, f(&#39;%+6d&#39;, 123));
<a name="line125"></a>  assertEquals(&#39;negative %+6d&#39;, &#39;-  123&#39;, f(&#39;%+6d&#39;, -123));
<a name="line126"></a>
<a name="line127"></a>  assertEquals(&#39;positive %+f &#39;, &#39;+123.456&#39;, f(&#39;%+f&#39;, 123.456));
<a name="line128"></a>  assertEquals(&#39;negative %+f &#39;, &#39;-123.456&#39;, f(&#39;%+f&#39;, -123.456));
<a name="line129"></a>
<a name="line130"></a>  assertEquals(&#39;positive %+.2f &#39;, &#39;+123.46&#39;, f(&#39;%+.2f&#39;, 123.456));
<a name="line131"></a>  assertEquals(&#39;negative %+.2f &#39;, &#39;-123.46&#39;, f(&#39;%+.2f&#39;, -123.456));
<a name="line132"></a>
<a name="line133"></a>  assertEquals(&#39;positive %+6.2f&#39;, &#39;+123.46&#39;, f(&#39;%+6.2f&#39;, 123.456));
<a name="line134"></a>  assertEquals(&#39;negative %+6.2f&#39;, &#39;-123.46&#39;, f(&#39;%+6.2f&#39;, -123.456));
<a name="line135"></a>
<a name="line136"></a>  assertEquals(&#39;positive %+7.2f&#39;, &#39;+123.46&#39;, f(&#39;%+7.2f&#39;, 123.456));
<a name="line137"></a>  assertEquals(&#39;negative %+7.2f&#39;, &#39;-123.46&#39;, f(&#39;%+7.2f&#39;, -123.456));
<a name="line138"></a>
<a name="line139"></a>  assertEquals(&#39;positive %+10.2f&#39;, &#39;+   123.46&#39;, f(&#39;%+10.2f&#39;, 123.456));
<a name="line140"></a>  assertEquals(&#39;negative %+10.2f&#39;, &#39;-   123.46&#39;, f(&#39;%+10.2f&#39;, -123.456));
<a name="line141"></a>
<a name="line142"></a>  assertEquals(&#39;string %+s &#39;, &#39;abc&#39;, f(&#39;%+s&#39;, &#39;abc&#39;));
<a name="line143"></a>  assertEquals(&#39;string %+3s&#39;, &#39;abc&#39;, f(&#39;%+3s&#39;, &#39;abc&#39;));
<a name="line144"></a>  assertEquals(&#39;string %+4s&#39;, &#39; abc&#39;, f(&#39;%+4s&#39;, &#39;abc&#39;));
<a name="line145"></a>  assertEquals(&#39;string %+6s&#39;, &#39;   abc&#39;, f(&#39;%+6s&#39;, &#39;abc&#39;));
<a name="line146"></a>}
<a name="line147"></a>
<a name="line148"></a>function testPrecision() {
<a name="line149"></a>  assertEquals(&#39;%.5d&#39;, &#39;0&#39;, f(&#39;%.5d&#39;, 0));
<a name="line150"></a>
<a name="line151"></a>  assertEquals(&#39;%d&#39;, &#39;123&#39;, f(&#39;%d&#39;, 123.456));
<a name="line152"></a>  assertEquals(&#39;%.2d&#39;, &#39;123&#39;, f(&#39;%.2d&#39;, 123.456));
<a name="line153"></a>
<a name="line154"></a>  assertEquals(&#39;%f&#39;, &#39;123.456&#39;, f(&#39;%f&#39;, 123.456));
<a name="line155"></a>  assertEquals(&#39;%.2f&#39;, &#39;123.46&#39;, f(&#39;%.2f&#39;, 123.456));
<a name="line156"></a>
<a name="line157"></a>  assertEquals(&#39;%.3f&#39;, &#39;123.456&#39;, f(&#39;%.3f&#39;, 123.456));
<a name="line158"></a>  assertEquals(&#39;%.6f&#39;, &#39;123.456000&#39;, f(&#39;%.6f&#39;, 123.456));
<a name="line159"></a>  assertEquals(&#39;%1.2f&#39;, &#39;123.46&#39;, f(&#39;%1.2f&#39;, 123.456));
<a name="line160"></a>  assertEquals(&#39;%7.2f&#39;, &#39; 123.46&#39;, f(&#39;%7.2f&#39;, 123.456));
<a name="line161"></a>
<a name="line162"></a>  assertEquals(&#39;%5.6f&#39;, &#39;123.456000&#39;, f(&#39;%5.6f&#39;, 123.456));
<a name="line163"></a>  assertEquals(&#39;%11.6f&#39;, &#39; 123.456000&#39;, f(&#39;%11.6f&#39;, 123.456));
<a name="line164"></a>
<a name="line165"></a>  assertEquals(&#39;%07.2f&#39;, &#39;0123.46&#39;, f(&#39;%07.2f&#39;, 123.456));
<a name="line166"></a>  assertEquals(&#39;%+7.2f&#39;, &#39;+123.46&#39;, f(&#39;%+7.2f&#39;, 123.456));
<a name="line167"></a>}
<a name="line168"></a>
<a name="line169"></a>function testZeroFlag() {
<a name="line170"></a>  assertEquals(&#39;%0s&#39;, &#39;abc&#39;, f(&#39;%0s&#39;, &#39;abc&#39;));
<a name="line171"></a>  assertEquals(&#39;%02s&#39;, &#39;abc&#39;, f(&#39;%02s&#39;, &#39;abc&#39;));
<a name="line172"></a>  assertEquals(&#39;%06s&#39;, &#39;   abc&#39;, f(&#39;%06s&#39;, &#39;abc&#39;));
<a name="line173"></a>
<a name="line174"></a>  assertEquals(&#39;%0d&#39;, &#39;123&#39;, f(&#39;%0d&#39;, 123));
<a name="line175"></a>  assertEquals(&#39;%0d&#39;, &#39;-123&#39;, f(&#39;%0d&#39;, -123));
<a name="line176"></a>  assertEquals(&#39;%06d&#39;, &#39;000123&#39;, f(&#39;%06d&#39;, 123));
<a name="line177"></a>  assertEquals(&#39;%06d&#39;, &#39;-00123&#39;, f(&#39;%06d&#39;, -123));
<a name="line178"></a>  assertEquals(&#39;%010d&#39;, &#39;0000000123&#39;, f(&#39;%010d&#39;, 123));
<a name="line179"></a>  assertEquals(&#39;%010d&#39;, &#39;-000000123&#39;, f(&#39;%010d&#39;, -123));
<a name="line180"></a>}
<a name="line181"></a>
<a name="line182"></a>function testFlagMinus() {
<a name="line183"></a>  assertEquals(&#39;%-s&#39;, &#39;abc&#39;, f(&#39;%-s&#39;, &#39;abc&#39;));
<a name="line184"></a>  assertEquals(&#39;%-2s&#39;, &#39;abc&#39;, f(&#39;%-2s&#39;, &#39;abc&#39;));
<a name="line185"></a>  assertEquals(&#39;%-6s&#39;, &#39;abc   &#39;, f(&#39;%-6s&#39;, &#39;abc&#39;));
<a name="line186"></a>
<a name="line187"></a>  assertEquals(&#39;%-d&#39;, &#39;123&#39;, f(&#39;%-d&#39;, 123));
<a name="line188"></a>  assertEquals(&#39;%-d&#39;, &#39;-123&#39;, f(&#39;%-d&#39;, -123));
<a name="line189"></a>  assertEquals(&#39;%-2d&#39;, &#39;123&#39;, f(&#39;%-2d&#39;, 123));
<a name="line190"></a>  assertEquals(&#39;%-2d&#39;, &#39;-123&#39;, f(&#39;%-2d&#39;, -123));
<a name="line191"></a>  assertEquals(&#39;%-4d&#39;, &#39;123 &#39;, f(&#39;%-4d&#39;, 123));
<a name="line192"></a>  assertEquals(&#39;%-4d&#39;, &#39;-123&#39;, f(&#39;%-4d&#39;, -123));
<a name="line193"></a>
<a name="line194"></a>  assertEquals(&#39;%-d&#39;, &#39;123&#39;, f(&#39;%-0d&#39;, 123));
<a name="line195"></a>  assertEquals(&#39;%-d&#39;, &#39;-123&#39;, f(&#39;%-0d&#39;, -123));
<a name="line196"></a>  assertEquals(&#39;%-4d&#39;, &#39;123 &#39;, f(&#39;%-04d&#39;, 123));
<a name="line197"></a>  assertEquals(&#39;%-4d&#39;, &#39;-123&#39;, f(&#39;%-04d&#39;, -123));
<a name="line198"></a>}
<a name="line199"></a>
<a name="line200"></a>function testExceptions() {
<a name="line201"></a>  var e = assertThrows(goog.partial(f, &#39;%f%f&#39;, 123.456));
<a name="line202"></a>  assertEquals(&#39;[goog.string.format] Not enough arguments&#39;, e.message);
<a name="line203"></a>
<a name="line204"></a>  e = assertThrows(f);
<a name="line205"></a>  assertEquals(&#39;[goog.string.format] Template required&#39;, e.message);
<a name="line206"></a>}
<a name="line207"></a>
<a name="line208"></a>function testNonParticipatingGroupHandling() {
<a name="line209"></a>  // Firefox supplies empty string instead of undefined for non-participating
<a name="line210"></a>  // capture groups. This can trigger bad behavior if a demuxer only checks
<a name="line211"></a>  // isNaN(val) and not also val == &#39;&#39;. Check for regressions.
<a name="line212"></a>  var format = &#39;%s %d %i %u&#39;;
<a name="line213"></a>  var expected = &#39;1 2 3 4&#39;;
<a name="line214"></a>  // Good types
<a name="line215"></a>  assertEquals(expected, goog.string.format(format, 1, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;));
<a name="line216"></a>  // Bad types
<a name="line217"></a>  assertEquals(expected, goog.string.format(format, &#39;1&#39;, 2, 3, 4));
<a name="line218"></a>}
</pre>


</body>
</html>
